package com.ruoyi.storage.packageWeighing.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ruoyi.basicData.product.domain.Product;
import com.ruoyi.basicData.product.mapper.ProductMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.outbound.outbound.domain.Outbound;
import com.ruoyi.outbound.outbound.mapper.OutboundMapper;
import com.ruoyi.storage.bicycle.domain.Bicycle;
import com.ruoyi.storage.bicycle.mapper.BicycleMapper;
import com.ruoyi.storage.fitting.domain.FittingValue;
import com.ruoyi.storage.fitting.mapper.FittingMapper;
import com.ruoyi.storage.tickets.domain.Tickets;
import com.ruoyi.storage.tickets.mapper.TicketsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.packageWeighing.mapper.PackageMapper;
import com.ruoyi.storage.packageWeighing.domain.Package;
import com.ruoyi.storage.packageWeighing.service.IPackageService;

/**
 * 包装称重Service业务层处理
 * 
 * @author zzx
 * @date 2024-02-04
 */
@Service
public class PackageServiceImpl implements IPackageService 
{
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private TicketsMapper ticketsMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BicycleMapper bicycleMapper;
    @Autowired
    private OutboundMapper outboundMapper;
    @Autowired
    private FittingMapper fittingMapper;

    /**
     * 查询包装称重
     * 
     * @param bicycleId 包装称重主键
     * @return 包装称重
     */
    @Override
    public Package selectPackageByBicycleId(String bicycleId)
    {
        Package packagei = packageMapper.selectPackageByBicycleId(bicycleId);
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(packagei.getOrderId());
        Product product = productMapper.selectProductByProductId(tickets.getProductId());
        packagei.setProductId(product.getProductId());
        packagei.setProductName(product.getProductName());
        packagei.setSpecifications(product.getSpecifications());
        packagei.setCustomerModel(product.getCustomerModel());
        packagei.setColor(tickets.getColour());
        packagei.setClientName(tickets.getClientName());
        packagei.setClientOrderNumber(tickets.getClientOrderNumber());
        packagei.setWarehouse(tickets.getWarehouseId());
        return packagei;
    }

    /**
     * 查询包装称重列表
     * 
     * @param packagea 包装称重
     * @return 包装称重
     */
    @Override
    public List<Package> selectPackageList(Package packagea)
    {
        List<Package> packages = packageMapper.selectPackageList(packagea);
//        for (Package packagei: packages) {
//            Tickets tickets = ticketsMapper.selectTicketsByTicketsId(packagei.getOrderId());
//            Product product = productMapper.selectProductByProductId(tickets.getProductId());
//            packagei.setProductId(product.getProductId());
//            packagei.setProductName(product.getProductName());
//            packagei.setSpecifications(product.getSpecifications());
//            packagei.setCustomerModel(product.getCustomerModel());
//            packagei.setColour(tickets.getColour());
//            packagei.setClientName(tickets.getClientName());
//            packagei.setClientOrderNumber(tickets.getClientOrderNumber());
//            packagei.setWarehouse(tickets.getWarehouseId());
//        }
        return packages;
    }

    /**
     * 新增包装称重
     * 
     * @param packagew 包装称重
     * @return 结果
     */
    @Override
    public int insertPackage(Package packagew)
    {
        packagew.setPackagingFlag(2L);
        Bicycle bicycle = bicycleMapper.selectBicycleByBicycleId(packagew.getBicycleId());
        if (bicycle.getAudit()==null){
            packagew.setAudit(0L);
        }
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(bicycle.getOrderId());
        tickets.setQuantityPacked(tickets.getQuantityPacked()+1L);
        packagew.setStatus(3L);
        ticketsMapper.updateTickets(tickets);
        System.out.println(packagew);
        return packageMapper.updatePackage(packagew);
    }

    /**
     * 修改包装称重
     * 
     * @param packagew 包装称重
     * @return 结果
     */
    @Override
    public int updatePackage(Package packagew)
    {

        return packageMapper.updatePackage(packagew);
    }

    /**
     * 批量删除包装称重
     * 
     * @param bicycleIds 需要删除的包装称重主键
     * @return 结果
     */
    @Override
    public int deletePackageByBicycleIds(String[] bicycleIds)
    {
        return packageMapper.deletePackageByBicycleIds(bicycleIds);
    }

    /**
     * 删除包装称重信息
     * 
     * @param bicycleId 包装称重主键
     * @return 结果
     */
    @Override
    public int deletePackageByBicycleId(String bicycleId)
    {
        return packageMapper.deletePackageByBicycleId(bicycleId);
    }
    /**
     * 序列号追询
     * @param packagew
     * @return
     */
    @Override
    public List<Package> selectPBicycleList(Package packagew) {
        List<Package> packages = packageMapper.selectBicycleList(packagew);
        for (Package bicycleListVo : packages) {
            List<FittingValue> fittingValues = fittingMapper.selectFittingValueByBicycle(bicycleListVo.getBicycleId());
            Map<String, String> map = new HashMap<>();
            for (FittingValue fittingValue : fittingValues) {
                map.put(fittingValue.getFittingFabienName(), fittingValue.getFitting());
            }
            bicycleListVo.setMap(map);
        }
            return packages;

    }
    @Override
    public AjaxResult selectPackageStatusByBicycleId(String bicycleId) {
        System.out.println(bicycleId);
        int a=packageMapper.selectStatusByBicycleId(bicycleId);
        if (a==0){
            return AjaxResult.error(500,"采集未完成");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateBicycleAudit(String[] bicycleIds) {
        packageMapper.updateAudit(bicycleIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateBicycleUnAudit(String[] bicycleIds) {
        packageMapper.updateUnAudit(bicycleIds);
        return AjaxResult.success();
    }
}
